package bitoperations;

//判断2次幂4次幂
public class powerOf2 {
    public static void main(String[] args) {
        System.out.println(isPowerOf2(4));
        System.out.println(isPowerOf2(5));
        System.out.println(isPowerOf2_02(4));
        System.out.println(isPowerOf4(4));
        System.out.println(isPowerOf4(5));
    }

    public static boolean isPowerOf2(int n) {
        return (n & (n - 1)) == 0;
    }
    public static boolean isPowerOf2_02(int n) {
        int i = n & (~n + 1);
        return i == n;
    }

    public static boolean isPowerOf4(int n) {
       return isPowerOf2_02(n) && (n & 0x55555555) != 0;
    }
}
